package com.polaris.live.ui.main.fragment

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.network.parseState
import com.polaris.live.constant.UserRankConst
import com.polaris.live.databinding.FragmentDailyListBinding
import com.polaris.live.interface_bag.OnPartyGameSwitchListener
import com.polaris.live.resp.back_resp.PartyGameResp
import com.polaris.live.resp.bean.UserRankVo
import com.polaris.live.translation.R.string
import com.polaris.live.ui.main.adapter.DailyListAdapter
import com.polaris.live.viewmodel.login.UserRankViewModel
import com.polaris.live.widget.avatar.FrameImageUtils
import com.polaris.live.widget.refresh.EmptyView

/**
 * 日榜
 *
 * @author Created by lucas on 2023/10/30 17:16
 * @since 1.0.0
 */
class DailyListFragment : BaseVbFragment<UserRankViewModel, FragmentDailyListBinding>(),
    OnPartyGameSwitchListener {

    private var mType: Int = UserRankConst.TYPE_RICH
    private lateinit var mAdapter: DailyListAdapter

    private var mGameResp: PartyGameResp? = null

    private val mEmptyView by lazy { EmptyView(requireContext()) }

    override fun initView(savedInstanceState: Bundle?) {
        mType = arguments?.getInt("type") ?: UserRankConst.TYPE_RICH
        mGameResp = arguments?.getParcelable("gameResp")

        val userResp = UserManager.getUser()
        mBinding.avFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(userResp.decoration?.avatarFrame))

        mBinding.ivUserHead.setImageUrl(userResp.avatar, userResp.userId)

        mBinding.tvUserName.text = userResp.nickname

        mBinding.tvUserName.setNicknameColor(
            userResp.decoration?.nicknameColor,
            R.color.color_b2ffffff
        )
        mBinding.tvCountry.setCountry(userResp.profile?.secondCountry)
        val sex = if (mGameResp != null) {
            UserManager.getUser().sex
        } else {
            when (mType) {
                UserRankConst.TYPE_CHARM -> AppConst.UserSex.WOMAN
                UserRankConst.TYPE_RICH -> AppConst.UserSex.MAN
                else -> UserManager.getUser().sex
            }
        }
        mBinding.vipView.setVipLevel(userResp.profile?.vipLevel)

        mBinding.tvLevel.setRichCharmBySexLevel(
            sex, userResp.account?.richs, userResp.account?.charms
        )

        mAdapter = DailyListAdapter(mType, mGameResp)

        mBinding.rvDailyList.adapter = mAdapter
        mBinding.rvDailyList.layoutManager = LinearLayoutManager(requireContext())
        val gameResp = mGameResp
        if (gameResp != null) {
            mViewModel.rankPartyGameDayLiveData.observe(this) { resultState ->
                parseState(resultState, {
                    handleRankData(it)
                })
            }
            mViewModel.getPartyGameDay(gameResp.id)
        } else {
            when (mType) {
                UserRankConst.TYPE_CHARM -> {//魅力榜
                    mViewModel.rankCharmDayLiveData.observe(this) { state ->
                        parseState(state, {
                            handleRankData(it)
                        })
                    }
                    mViewModel.getCharmDayRank()
                }

                UserRankConst.TYPE_RICH -> {//财富榜
                    mViewModel.rankRichDayLiveData.observe(this) { state ->
                        parseState(state, {
                            handleRankData(it)
                        })
                    }
                    mViewModel.getRichDayRank()
                }

                else -> {
                    mViewModel.rankGameDayLiveData.observe(this) { state ->
                        parseState(state, {
                            handleRankData(it)
                        })
                    }
                    mViewModel.getGameDayRank()
                }
            }
        }

        mViewModel.rankAnonymousLiveData.observe(this) {
            if (mGameResp != null) {
                gameResp?.id?.let { it1 -> mViewModel.getPartyGameDay(it1) }
            } else {
                when (mType) {
                    UserRankConst.TYPE_CHARM -> mViewModel.getCharmDayRank()//魅力榜
                    UserRankConst.TYPE_RICH -> mViewModel.getRichDayRank()//财富榜
                    else -> mViewModel.getGameDayRank()
                }
            }
        }

        mBinding.switchBtn.setOnCheckedChangeListener { buttonView, isSelected ->
            mBinding.checkbox.isSelected = isSelected

            if (buttonView.isPressed) {
                val gameResp = mGameResp
                val type = if (gameResp != null) {
                    mViewModel.getDayOrMonthType(gameResp, true)
                } else {
                    when (mType) {
                        UserRankConst.TYPE_CHARM -> UserRankConst.CHARM_DAY
                        UserRankConst.TYPE_RICH -> UserRankConst.RICH_DAY
                        else -> UserRankConst.GAME_DAY
                    }
                }
                mViewModel.rankAnonymous(if (isSelected) 0 else 1, type)
            }
        }
    }

    private fun handleRankData(data: UserRankVo) {

        if (CollectionUtils.isEmpty(data.users) && !mAdapter.hasEmptyView()) {
            mEmptyView.setEmptyTip(
                R.drawable.img_weishangbang,
                StringUtils.getString(string.no_hava_data),
                ColorUtils.getColor(R.color.color_ffffff_50),
                128, 139
            )
            mAdapter.setEmptyView(mEmptyView)
        }
        mAdapter.setList(data.users)

        mBinding.tvNum.text = if (data.selfRank <= 100 && data.selfRank != 0) {
            data.selfRank.toString()
        } else if (data.selfRank > 100) "100+" else "-"


        mBinding.tvCoins.text = data.selfScore.toString()
        if (mType != UserRankConst.TYPE_CHARM) {
            val drawable = context?.let { ContextCompat.getDrawable(it, R.drawable.icon_diamond) }
            mBinding.tvCoins.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null)
        }
        if (mGameResp != null) {
            mBinding.tvCoins.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        }

        mBinding.checkbox.isSelected = data.anonymous == 0
        mBinding.switchBtn.isChecked = data.anonymous == 0
    }

    override fun onGameSwitchCallback(gameResp: PartyGameResp) {
        if (gameResp.id == mGameResp?.id) return
        mGameResp = gameResp
        mViewModel.getPartyGameDay(gameResp.id)
    }

}